﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;
using TinyLib.Sprite;
using TinyLib.Entity;

namespace GodBuilderIdea.Tools
{
    public class MoveTribeTool : ITool
    {
        private List<Player> m_players;
        private Player m_player;
        private List<EnvTile> m_tiles;

        private Tribe m_tribe;

        /// <summary>
        /// Constructing the tool
        /// </summary>
        /// <param name="player">currently active player</param>
        /// <param name="tiles">list of all tiles on the board</param>
        public MoveTribeTool(Player player, List<Player> players, List<EnvTile> tiles)
        {
            m_player = player;
            m_players = players;
            m_tiles = tiles;
        }

        /// <summary>
        /// Handles mouse left clicking
        /// </summary>
        /// <param name="x">Mosue x coordinate.</param>
        /// <param name="y">Mouse y coordniate.</param>
        public override void MouseLeftClick(int x, int y)
        {
            Point mouseCursor = new Point(x, y);

            //find which tribe is clicked.
            foreach (Tribe tribe in m_player.m_tribes)
            {
                if (tribe.rectangle.Contains(mouseCursor))
                {
                    SelectTribe(tribe);

                    return;
                }
            }

            //If we have a tribe selected.
            if (m_tribe != null)
            {
                MoveTribe(x, y);
            }
        }

        /// <summary>
        /// Handles tribe selection.
        /// </summary>
        /// <param name="tribe">Trib to be selected.</param>
        private void SelectTribe(Tribe tribe)
        {
            if (m_tribe != null)
            {
                m_tribe.m_selected = false;
              //  return;
            }

            m_tribe = tribe;
            m_tribe.m_selected = true;
        }

        /// <summary>
        /// Handles tribe movement.
        /// </summary>
        /// <param name="x">Mouse x coordinate</param>
        /// <param name="y">Mouse y coordinate</param>
        private void MoveTribe(int x, int y)
        {
            if (m_activePlayer.m_actions < 5)
                return;

            Point mouseCursor = new Point(x, y);
            
            //if a tribe is selected
            if (m_tribe != null)
            {
                //Find clicked tile.
                //foreach (EnvTile tile in m_tiles)
                for (int i = 0; i < m_tiles.Count; i++)
                {
                    if (m_tiles[i].Rectangle.Contains(mouseCursor))
                    {
                        if (m_tiles[i].m_type == EnvTile.EnvironmentType.MOUNTAIN)
                            return;

                        foreach(Player player in m_players)
                            foreach(Tribe tribe in player.m_tribes)
                                if(tribe.rectangle.Contains(mouseCursor))
                                    return;
                        List<int> adjacent_tiles = m_tribe.GetTwoAdjacent(m_tiles);

                        for (int j = 0; j < adjacent_tiles.Count; j++)
                        {
                            if (i == adjacent_tiles[j]) //if the clicked tile is found in the adjacent list
                            {
                                //Move tribe.
                                m_tribe.position = m_tiles[i].position;
                                m_tribe.m_tile_index_pos = i;
                                m_tribe.m_selected = false;
                                m_activePlayer.m_actions -= 5;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Handles mouse right clicking
        /// </summary>
        /// <param name="x">Mosue x coordinate.</param>
        /// <param name="y">Mouse y coordniate.</param>
        public override void MouseRightClick(int x, int y)
        {
            
        }
    }
}
